<?php 

/*====================================================
 Author: RooTM
------------------------------------------------------
 Web-site: http://weboss.net/
=====================================================*/

@error_reporting ( E_ALL ^ E_WARNING ^ E_NOTICE );
@ini_set ( 'display_errors', true );
@ini_set ( 'html_errors', false );
@ini_set ( 'error_reporting', E_ALL ^ E_WARNING ^ E_NOTICE );

@session_start();

define( 'DATALIFEENGINE', true );
define( 'ROOT_DIR', '../../../..' );
define( 'ENGINE_DIR', '../../..' );

include ENGINE_DIR . '/data/config.php';
include ENGINE_DIR . '/modules/iChat/data/config.php';
require_once ENGINE_DIR.'/modules/iChat/language/website.lng';
require_once ENGINE_DIR.'/modules/iChat/functions.php';

if( $config['http_home_url'] == "" ) {
	
	$config['http_home_url'] = explode( "engine/modules/iChat/ajax/add.php", $_SERVER['PHP_SELF'] );
	$config['http_home_url'] = reset( $config['http_home_url'] );
	$config['http_home_url'] = "http://" . $_SERVER['HTTP_HOST'] . $config['http_home_url'];

}

require_once ENGINE_DIR . '/classes/mysql.php';
require_once ENGINE_DIR . '/data/dbconfig.php';
require_once ENGINE_DIR . '/modules/functions.php';

if (isset ( $_COOKIE['dle_skin'] ) ) {

	$_COOKIE['dle_skin'] = trim( totranslit($_COOKIE['dle_skin'], false, false) );

	if ($_COOKIE['dle_skin'] != '' AND @is_dir ( ROOT_DIR . '/templates/' . $_COOKIE['dle_skin'] )) {
		$config['skin'] = $_COOKIE['dle_skin'];
	}
}

if( $config["lang_" . $config['skin']] ) {
	if ( file_exists( ROOT_DIR . '/language/' . $config["lang_" . $config['skin']] . '/website.lng' ) ) {	
		include_once ROOT_DIR . '/language/' . $config["lang_" . $config['skin']] . '/website.lng';
	} else die("Language file not found");
} else {
	
	include_once ROOT_DIR . '/language/' . $config['langs'] . '/website.lng';

}

//################# Определение групп пользователей
$user_group = get_vars( "usergroup" );

if( ! $user_group ) {
	$user_group = array ();
	
	$db->query( "SELECT * FROM " . USERPREFIX . "_usergroups ORDER BY id ASC" );
	
	while ( $row = $db->get_row() ) {
		
		$user_group[$row['id']] = array ();
		
		foreach ( $row as $key => $value ) {
			$user_group[$row['id']][$key] = stripslashes($value);
		}
	
	}
	set_vars( "usergroup", $user_group );
	$db->free();
}

require_once ENGINE_DIR . '/modules/sitelogin.php';

if( ! $is_logged ) die( "error" );

$_POST['message'] = trim($_POST['message']);

$message = convert_unicode( $_POST['message'], $config['charset'] );

if(($member_id['user_group'] != 1)) {$chat_cfg['stop_bbcode'] = $chat_cfg['stop_bbcode'] . ",[admin],[/admin]";}

if( !$user_group[$member_id['user_group']]['allow_url'] ) {$chat_cfg['stop_bbcode'] = $chat_cfg['stop_bbcode'] . ",[url=,[/url],[leech=,[/leech]";}
		
$i = 0;
$ban_codes = explode(",",  $chat_cfg['stop_bbcode'] );
foreach($ban_codes as $ban_code){
$i++; $ban_code = trim($ban_code);
if(stristr(strtolower(stripslashes($message)), $ban_code)){
$error = $chat_lang['bad'];
break;
}}

if( dle_strlen( stripslashes($message), $config['charset'] ) > $chat_cfg['max_text'] ) {
	$error = $chat_lang['max'];
}

if( $_POST['message'] == '' OR $member_id['banned'] == "yes" ) {
	$error = $chat_lang['null'];
}

// Проверка защиты от флуда
if( $member_id['user_group'] > 2 and intval( $chat_cfg['stop_flood'] ) and ! $error) {
	if( flooder( $_IP, $chat_cfg['stop_flood'] ) == TRUE ) {
		$error =  $chat_lang['flood'];	
	}
}

if(md5($_POST['message']) == md5($_SESSION['last_message'])){
$error = $chat_lang['copy'];
}

if(($member_id['user_group'] == 1)) {
if (@file_exists(ROOT_DIR.'/iChat_install.php')) {
$error = $chat_lang['install_in'];
}}

if( ! $error ){
 
$_SESSION['last_message'] = $_POST['message'];

require_once ENGINE_DIR . '/classes/parse.class.php';

$parse = new ParseFilter( );
$parse->safe_mode = true;
$parse->allow_url = $user_group[$member_id['user_group']]['allow_url'];
$parse->allow_image = $user_group[$member_id['user_group']]['allow_image'];

$_TIME = time() + ($config['date_adjust'] * 60);
$_IP = $db->safesql( $_SERVER['REMOTE_ADDR'] );
$time = date( "Y-m-d H:i:s", $_TIME );

$message = $db->safesql( $parse->BB_Parse( $parse->process( $message ), false ) );

	$smilies_arr = explode( ",", $chat_cfg['smiles'] );
		foreach ( $smilies_arr as $smile ) {
			$smile = trim( $smile );
			$find[] = "':$smile:'";
			$replace[] = "<!--smile:{$smile}--><img style=\"vertical-align: middle;border: none;\" alt=\"$smile\" src=\"" . $config['http_home_url'] . "engine/data/emoticons/{$smile}.gif\" /><!--/smile-->";
		}

$message = preg_replace( $find, $replace, $message );

	//* Автоперенос длинных слов
		if( intval( $chat_cfg['max_word'] ) AND !$error ) {
			
			$message = preg_split( '((>)|(<))', $message, - 1, PREG_SPLIT_DELIM_CAPTURE );
			$n = count( $message );
			
			for($i = 0; $i < $n; $i ++) {
				if( $message[$i] == "<" ) {
					$i ++;
					continue;
				}
				
				$message[$i] = preg_replace( "#([^\s\n\r]{" . intval( $chat_cfg['max_word'] ) . "})#i", "\\1<br />", $message[$i] );
			}
			
			$message = join( "", $message );
		
		}

   
  	if( $chat_cfg['stop_flood'] ) {
			$db->query( "INSERT INTO " . PREFIX . "_flood (id, ip, flag) values ('$_TIME', '$_IP', '1')" );
		}  

  $db->query( "INSERT INTO " . PREFIX . "_iChat (author, user_group, message, date) values ('{$member_id['name']}', '{$member_id['user_group']}', '$message', '$time')" );  

clear_cache( 'iChat_' );

}

if ($config['allow_cache'] != "yes") { $config['allow_cache'] = "yes";}

$Messages = dle_cache( "iChat", $config['skin'] );

include ENGINE_DIR . '/modules/iChat/build.php';

@header( "Content-type: text/css; charset=" . $config['charset'] );

echo $Messages;

if(!$error) $js_c = "document.getElementById('iChat_form').message.value = '';";
       else $js_c = "DLEalert('".$error."', '".$chat_lang['title']."')";

echo '<script language="JavaScript" type="text/javascript">'.$js_c.'</script>';

?>
